package com.atguigu.juc.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author yfstart
 * @create 2022-06-27 18:25
 */
public class CompletableFutureAPIApplyToEither {
  public static void main(String[] args) {
    CompletableFuture<String> playA = CompletableFuture.supplyAsync(() -> {
      System.out.println("A come in");
      try { TimeUnit.MILLISECONDS.sleep(2000); } catch (InterruptedException e) { e.printStackTrace();}
      return "play A";
    });

    CompletableFuture<String> playB = CompletableFuture.supplyAsync(() -> {
      System.out.println("B come in");
      try { TimeUnit.MILLISECONDS.sleep(3000); } catch (InterruptedException e) { e.printStackTrace();}
      return "play B";
    });

    CompletableFuture<String> result = playA.applyToEither(playB, f -> {
      return f + " is winner";
    });

    System.out.println(Thread.currentThread().getName() + "\t ---:" + result.join());
  }
}
